package indi.chengx.walty.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowStateListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

import org.freedesktop.gstreamer.Bin;
import org.freedesktop.gstreamer.Element;
import org.freedesktop.gstreamer.Pipeline;
import org.freedesktop.gstreamer.elements.AppSink;
import org.freedesktop.gstreamer.lowlevel.GstBinAPI;

import indi.chengx.walty.Config;
import indi.chengx.walty.dirve.Camera;
import indi.chengx.walty.ui.lisener.StartVideoListener;
import indi.chengx.walty.ui.lisener.TakePicture;
import indi.chengx.walty.ui.lisener.WindowListener;
import indi.chengx.walty.util.Console;

public class WqFrame{
	private final String TAG = this.getClass().getSimpleName();
	private Camera camera;
	
	public WqFrame() {
		camera = new Camera();
	}
	
	public void initFrame() {
		Console.log(TAG, "initFrame");
		JFrame f = new JFrame("XEnode");
		JPanel mainPanel = new JPanel();
        JPanel topPanel = this.initTopPanel();
        JPanel bottomPanel = this.initBottomPanel();
        
        mainPanel.setLayout(new BorderLayout());
		mainPanel.add(topPanel,BorderLayout.CENTER);
        mainPanel.add(bottomPanel,BorderLayout.SOUTH);
        
        f.add(mainPanel);
        f.setSize(Config.CAP_WIDTH, Config.CAP_HEIGHT + 50);
        f.pack();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setVisible(true);
        WindowListener w = new WindowListener(camera);
        f.addWindowStateListener(w);
        f.addKeyListener(w);
        
        camera.playVideo();
	}
	
	private JPanel initTopPanel() {
		JPanel topPanel = new JPanel();
		//tcambin name=src ! tee ! queue  ! valve name=valve ! videoconvert ! jpegenc  ! multifilesink  name=filesink post-messages=true  location="+IMAGE_SAVE_PATH+" ! queue ! videoconvert ! capsfilter caps=video/x-raw,width="+CAP_WIDTH+",height=" + CAP_HEIGHT + " ! appsink name=appsink";
		SimpleVideoComponent vc = new SimpleVideoComponent((AppSink) camera.getPipe().getElementByName("appsink"));
		vc.setPreferredSize(new Dimension(Config.CAP_WIDTH, Config.CAP_HEIGHT));
        topPanel.add(vc);
        //camera.readyVideo();
        //camera.pauseVideo();
        Console.log(TAG, "initTopPanel");
        return topPanel;
	}
	
	private JPanel initBottomPanel() {
		JPanel bottomPanel = new JPanel();
		bottomPanel.setLayout(new GridLayout(1,1));
		//JButton cBtn = new JButton("play");
		JButton pictureBtn = new JButton("save");
       // JButton printBtn = new JButton("print");
        //cBtn.addMouseListener(new StartVideoListener(camera,cBtn));
        pictureBtn.addMouseListener(new TakePicture(camera));
        pictureBtn.addKeyListener(new WindowListener(camera));
        //bottomPanel.add(cBtn);
        bottomPanel.add(pictureBtn);
        //bottomPanel.add(printBtn);
        Console.log(TAG, "initBottomPanel");
        return bottomPanel;
	}
	
}
